#include <MyDialog.h>
#include <QtGui>
#include <MyButton.h>

MyDialog::MyDialog(QWidget *parent)
    :QWidget(parent){

    QPushButton *chooseButton1 = new QPushButton(tr("&Choose"));
    QPushButton *chooseButton2 = new QPushButton(tr("C&hoose"));
    QLabel *processMentionLabel = new QLabel;
    processButton = new MyButton();
    processButton->setMentionLabel(processMentionLabel);
    processButton->setText(tr("P&rocess"));
    processButton->setDisabled(true);

    QShortcut *shortcut1 = new QShortcut(QKeySequence("Alt+1"),this);
    QShortcut *shortcut2 = new QShortcut(QKeySequence("Alt+2"),this);

    filePathLabel1 = new QLabel;
    filePathLabel1->setText("File<u>1</u>:");
    filePathLabel2 = new QLabel;
    filePathLabel2->setText("File<u>2</u>:");

    filePathText1 = new QLineEdit;
    filePathText2 = new QLineEdit;

    connect(chooseButton1, SIGNAL(clicked()), this, SLOT(setFilePath1()));
    connect(chooseButton2, SIGNAL(clicked()), this, SLOT(setFilePath2()));
    connect(processButton, SIGNAL(clicked()), this, SLOT(processFiles()));
    connect(shortcut1, SIGNAL(activated()), this, SLOT(focusFilePathText1()));
    connect(shortcut2, SIGNAL(activated()), this, SLOT(focusFilePathText2()));
    connect(filePathText1, SIGNAL(textChanged(const QString &)), this, SLOT(judgeText()));
    connect(filePathText2, SIGNAL(textChanged(const QString &)), this, SLOT(judgeText()));

    QGridLayout *layout = new QGridLayout;
    layout->setColumnStretch(1, 1);
    layout->setColumnMinimumWidth(1, 250);
    layout->addWidget(filePathLabel1, 0, 0);
    layout->addWidget(filePathText1, 0, 1);
    layout->addWidget(chooseButton1, 0, 2);
    layout->addWidget(filePathLabel2, 1, 0);
    layout->addWidget(filePathText2, 1, 1);
    layout->addWidget(chooseButton2, 1, 2);
    layout->addWidget(processButton, 2, 2);
    layout->addWidget(processMentionLabel, 2, 1);

    setLayout(layout);
    setWindowTitle(tr("Reject Duplicates"));

}

void MyDialog::setFilePath(QLineEdit *filePathText){

    QFileDialog::Options options;
    QString selectedFilter;
    QString fileName = QFileDialog::getOpenFileName(this,
                                tr("QFileDialog::getOpenFileName()"),
                                filePathText->text(),
                                tr("All Files (*);;Text Files (*.txt)"),
                                &selectedFilter,
                                options);
    if (!fileName.isEmpty())
        filePathText->setText(fileName);
}

void MyDialog::setFilePath1(){
    setFilePath(filePathText1);
}
void MyDialog::setFilePath2(){
    setFilePath(filePathText2);
}

void MyDialog::judgeText(){
    if(filePathText1->text() == NULL || filePathText2->text() == NULL){
        processButton->setDisabled(true);
    }else{
        processButton->setDisabled(false);
    }
}

void MyDialog::focusFilePathText1(){
     filePathText1->setFocus();
}
void MyDialog::focusFilePathText2(){
     filePathText2->setFocus();
}

void MyDialog::processFiles(){
    QString filePath1 = filePathText1->text();
    QString filePath2 = filePathText2->text();
    QString backupPath1 = filePath1;
    QString backupPath2 = filePath2;
    backupPath1.append(".bak");
    backupPath2.append(".bak");

    QFile file1(filePath1);
    QFile file2(filePath2);
    QFile fileBak1(backupPath1);
    QFile fileBak2(backupPath2);

    if(!file1.exists() || !file2.exists()){
        QMessageBox::about(NULL, "Failed", "Can't find the files.");
        return;
    }

    if(fileBak1.exists()){
        fileBak1.remove();
    }
    if(fileBak2.exists()){
        fileBak2.remove();
    }

    if(!file1.copy(filePath1, backupPath1)){
        QMessageBox::about(NULL, "Failed", "Failed to backup file1.");
        qDebug()<<"Backup failed!"<<endl;
        return;
    }
    if(!file2.copy(filePath2, backupPath2)){
        QMessageBox::about(NULL, "Failed", "Failed to backup file2.");
        return;
    }
    if(!file1.remove() || !file2.remove()){
        QMessageBox::about(NULL, "Failed", "Failed to remove original files.");
        return;
    }

    if(!file1.open(QIODevice::ReadWrite | QIODevice::Text) ||
            !file2.open(QIODevice::ReadWrite | QIODevice::Text)) {

        QMessageBox::about(NULL, "Failed", "Can't open the files!");
        fileBak1.copy(backupPath1, filePath1);
        fileBak2.copy(backupPath2, filePath2);
        return;
    }

    if(!fileBak1.open(QIODevice::ReadWrite | QIODevice::Text) ||
            !fileBak2.open(QIODevice::ReadWrite | QIODevice::Text)){

        QMessageBox::about(NULL, "Failed", "Can't open the backup files!");
        file1.close();
        fileBak1.close();
        file2.close();
        fileBak2.close();
    }

    QTextStream inStream1(&file1);
    QTextStream inStream2(&file2);
    QTextStream outStream1(&fileBak1);
    QTextStream outStream2(&fileBak2);
    QMap<QString, QString> map1;
    QMap<QString, QString> map2;
    QMap<QString, QString>::const_iterator i;
    QString line;

    outStream1.seek(0);
    while( !outStream1.atEnd()){
        line = outStream1.readLine();
        if(line == "")
            continue;
        map1.insert(line, line);
    }

    outStream2.seek(0);
    while( !outStream2.atEnd()){
        line = outStream2.readLine();
        if(line == "")
            continue;
        map2.insert(line, line);
    }

    if(map1.size() > map2.size()){
        for( i=map2.constBegin(); i!=map2.constEnd(); ++i){
            QString key = i.key();
            if(map1.contains(key)){
                map1.remove(key);
                map2.remove(key);
            }
        }
    }else{
        for( i=map1.constBegin(); i!=map1.constEnd(); ++i){
            QString key = i.key();
            if(map2.contains(key)){
                map1.remove(key);
                map2.remove(key);
            }
        }
    }

    inStream1.seek(0);
    for( i=map1.constBegin(); i!=map1.constEnd(); ++i)
        inStream1 << i.key() << endl;

    inStream2.seek(0);
    for( i=map2.constBegin(); i!=map2.constEnd(); ++i)
        inStream2 << i.key() << endl;

    QMessageBox::about(NULL, "", "Completed");

}

